import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../zujian/Login.vue'
import Home from '../zujian/Home.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path: '/home', component: Home }
  ]
})
//路由守卫
router.beforeEach((to, from, next) => {
  //to,将要访问的路径
  //from,从哪里跳转来的
  //next,是一个函数，表示放行
  if (to.path === "/login") {
    next();
  } else {
    const tokenStr = window.sessionStorage.getItem("token");
    if (!tokenStr) {
      next("/login");
    } else {
      next();
    }
  }
})

export default router